$(function () {

});

const vm = new Vue({
    el: '#rrapp',
    data: {
        select: {
            tag: {
                index: 0,
                name: null
            },
            paths: [{
                title: "",
                method: "",
                url: "",
                details: "",
                deprecated: false,
                produces: "",
                parameters: {},
                responses: {}
            }]// 选中的接口数据
        },
        swagger: {
            url: "http://127.0.0.1/"
        },
        apiDocs: {
            "swagger": "2.0",
            "info": {
                "description": "金字塔网络科技公司",
                "version": "1.0",
                "title": "接口文档",
                "termsOfService": "https://www.luxor-cms.cn/"
            },
            "host": "127.0.0.1",
            "basePath": "/",
            "tags": [
                {
                    "name": "_status",
                    "description": "状态码列表"
                }
            ],
            "produces": [
                "application/json"
            ],
            "paths": {
                "/status": {
                    "options": {
                        "tags": [
                            "_status"
                        ],
                        "summary": "状态码列表",
                        "operationId": "requireStatusCodesUsingOPTIONS",
                        "consumes": [
                            "application/json"
                        ],
                        "responses": {
                            "-1": {
                                "description": "执行失败"
                            },
                            "-2": {
                                "description": "服务异常,请联系管理员"
                            },
                            "-3": {
                                "description": "内部支撑服务不可用"
                            },
                            "0": {
                                "description": "执行成功"
                            },
                            "10000": {
                                "description": "服务繁忙,请稍后再试"
                            },
                            "10001": {
                                "description": "禁止重复消费幂等接口"
                            },
                            "10002": {
                                "description": "请求参数不合法"
                            },
                            "10003": {
                                "description": "参数类型错误"
                            },
                            "10004": {
                                "description": "JSON解析出错"
                            },
                            "11000": {
                                "description": "SQL语句执行出错"
                            },
                            "11001": {
                                "description": "事务回滚异常"
                            },
                            "11002": {
                                "description": "数据库中已存在该记录"
                            },
                            "11003": {
                                "description": "数据不存在"
                            },
                            "14400": {
                                "description": "token无效或已过期"
                            },
                            "200": {
                                "description": "OK"
                            },
                            "400": {
                                "description": "无效授权"
                            },
                            "401": {
                                "description": "未授权访问"
                            },
                            "403": {
                                "description": "禁止访问"
                            },
                            "404": {
                                "description": "内容不存在"
                            },
                            "405": {
                                "description": "方法不支持"
                            },
                            "408": {
                                "description": "服务繁忙,请稍后再试"
                            },
                            "500": {
                                "description": "服务不可用"
                            }
                        },
                        "security": [
                            {
                                "api_key": [
                                    "global"
                                ]
                            }
                        ],
                        "deprecated": false
                    }
                }
            },
            "securityDefinitions": {
                "api_key": {
                    "type": "apiKey",
                    "name": "Authorization",
                    "in": "header"
                }
            }
        }
    },
    mounted: function () {
        this.save();
    },
    methods: {
        generatorWord: function () {
            alert('生成接口文档...')
        },
        reset: function () {
            this.swagger.url = "http://127.0.0.1"
        },
        save: function () {
            let resourceUrl = "/swagger-resources"
            let apiDocsUrl = "/v2/api-docs"
            $.get(this.swagger.url + resourceUrl, function (data, status) {
                if (status == "success") {
                    apiDocsUrl = data[0].url
                }
            });
            $.get(this.swagger.url + apiDocsUrl, function (data, status) {
                if (status !== "success") {
                    alert("网络异常，请稍后再试")
                    return;
                } else {
                    vm.apiDocs = data
                    vm.checkTag(vm.apiDocs.tags[0].name, 0);
                }
            });
        },
        checkTag: function (name, index) {
            // 选中标签
            this.select.tag.index = index
            this.select.tag.name = name
            this.select.paths = []
            // 获取标签下的接口列表
            let urls = Object.keys(this.apiDocs.paths);
            for (const urlIndex in urls) {
                let path = {
                    method: "",
                    url: "",
                    title: "",
                    produces: ""
                }
                if ("_status" == urls[urlIndex]) {
                    continue
                }
                path.url = urls[urlIndex];
                path.method = Object.keys(this.apiDocs.paths[path.url])[0]
                path.title = this.apiDocs.paths[path.url][path.method].summary
                path.produces = this.apiDocs.produces[0]
                let tags = this.apiDocs.paths[path.url][path.method].tags
                for (const tagIndex in tags) {
                    let tagName = tags[tagIndex]
                    if (tagName == name) this.select.paths.push(path)
                }
            }
            console.log(this.select.paths)
        }
    }
});

